package com.ivens.adapters;

import java.util.Calendar;
import java.util.Date;

import android.content.Context;
import android.content.res.ColorStateList;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
import android.view.ViewGroup;
import android.widget.AbsoluteLayout;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.FrameLayout;
import android.widget.RelativeLayout;

import com.ivens.CalendarActivity;
import com.ivens.R;

// ///////////////////////////////////////////////////////////////////////////////////////
// Inner Class
public class CalendarAdapter extends BaseAdapter implements OnClickListener,
		OnTouchListener {

	private long[] calendar;
	private Context _context;
	private Button gridcell;
	String[] days = { "Dom", "Lun", "Mar", "Mie", "Jue", "Vie", "Sab" };
	java.util.Calendar selectedMonth;

	public CalendarAdapter(Context context, CalendarActivity adaptee,
			int textViewResourceId, int month, int year) {
		super();
		this.adaptee = adaptee;
		this._context = context;
		calendar = new long[49];
		selectedMonth = java.util.Calendar.getInstance();
		createCalendar();

	}

	public java.util.Calendar getSelectedMonth() {
		return selectedMonth;
	}

	public void setSelectedMonth(java.util.Calendar selectedMonth) {
		this.selectedMonth = selectedMonth;
		createCalendar();
	}

	public long[] createCalendar() {
		int month = selectedMonth.get(Calendar.MONTH);
		int year = selectedMonth.get(Calendar.YEAR);
		java.util.Calendar cal = java.util.Calendar.getInstance();
		cal.set(java.util.Calendar.YEAR, year);
		cal.set(java.util.Calendar.MONTH, month);
		cal.set(java.util.Calendar.DAY_OF_MONTH, 1);

//		System.out.println(cal.get(java.util.Calendar.MONTH));
//		System.out.println(cal.get(java.util.Calendar.DAY_OF_WEEK));

		for (int i = 0; i < days.length; i++) {
			calendar[i] = i;
		}
		System.out.println();
		int j = 0;
		for (int i = 7; i < 49; i++) {
			if (j < 7) {
				j++;
				if ((i - 7) >= (cal.get(java.util.Calendar.DAY_OF_WEEK) - 1)) {
//					System.out.print(cal.get(java.util.Calendar.DAY_OF_MONTH)
//							+ "\t");
					calendar[i] = cal.getTimeInMillis();
					cal.add(java.util.Calendar.DAY_OF_YEAR, 1);
				} else {
					cal.add(java.util.Calendar.DAY_OF_YEAR,
							(cal.get(java.util.Calendar.DAY_OF_WEEK) - 1) * -1);
					j--;
					for (int k = 0; k < cal.get(java.util.Calendar.DAY_OF_WEEK) - 1; k++) {
						if (j < 7) {
							j++;
//							System.out.print(cal
//									.get(java.util.Calendar.DAY_OF_MONTH)
//									+ "\t");
							calendar[k] = cal.getTimeInMillis();
							cal.add(java.util.Calendar.DAY_OF_YEAR, 1);
						} else {
							System.out.println();
						}
					}
					i = (cal.get(java.util.Calendar.DAY_OF_WEEK) - 2) + 7;

				}
			} else {
//				System.out.println();
				j = 0;
				i = i - 1;
			}
		}
		return calendar;
	}

	public int getCount() {
		// TODO Auto-generated method stub
		return calendar.length;
	}

	public Object getItem(int arg0) {
		// TODO Auto-generated method stub
		return null;
	}

	public long getItemId(int arg0) {
		// TODO Auto-generated method stub
		return 0;
	}

	boolean eom = false;
	boolean bom = false;
	private CalendarActivity adaptee;

	public View getView(int position, View convertView, ViewGroup parent) {
		View row = convertView;
		if (row == null) {
			LayoutInflater inflater = (LayoutInflater) _context
					.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
			row = inflater.inflate(R.layout.day_gridcell, parent, false);
		}
		gridcell = (Button) row.findViewById(R.id.calendar_day_gridcell);
		gridcell.setOnClickListener(this);
		gridcell.setOnTouchListener(this);
		Calendar dayOCal = Calendar.getInstance();
		dayOCal.setTime(new Date(calendar[position]));
		if (position < 7) {
			gridcell.setText(days[(int) calendar[position]]);
			gridcell.setEnabled(false);
			gridcell.setTextColor(Color.BLUE);
			gridcell.setTextSize(18);
		} else {
			gridcell.setTextSize(20);
			System.out.println(new Date(calendar[position]));
			gridcell.setText("" + dayOCal.get(Calendar.DAY_OF_MONTH));
			gridcell.setTag(calendar[position]);
			if (selectedMonth.get(Calendar.MONTH) == dayOCal
					.get(Calendar.MONTH)) {
				
				gridcell.setTextColor(0xffCCCC22);

			} else {
				gridcell.setTextColor(0xffCCCC99);
			}
			Calendar currentCal = Calendar.getInstance();
			if (currentCal.get(Calendar.YEAR) == dayOCal.get(Calendar.YEAR))
				if (currentCal.get(Calendar.MONTH) == dayOCal
						.get(Calendar.MONTH)) {
					if (currentCal.get(Calendar.DAY_OF_MONTH) == dayOCal
							.get(Calendar.DAY_OF_MONTH)) {
						gridcell.setTextColor(Color.RED);

					}
				}

		}
		return row;
	}

	public void onClick(View view) {
		adaptee.onDaySelected((Button) view);
	}

	Button day;
	ColorStateList mList;
	int color;
	float size;

	public boolean onTouch(View v, MotionEvent event) {
//		Button dayHi = (Button) this.adaptee.findViewById(R.id.floating_button);
//		if (event.getAction() == MotionEvent.ACTION_DOWN) {
//			day = (Button) v;
//			dayHi.setText(day.getText());
//			 float x=event.getX(0); 
//			 float y=event.getY(0);
//			dayHi.setLayoutParams(new RelativeLayout.LayoutParams());
//			dayHi.setTextSize(25);
//			dayHi.setVisibility(Button.VISIBLE);
//		} else {
//			dayHi.setVisibility(Button.INVISIBLE);
//		}
//
		return false;
	}

}